package LeetCode;

import java.util.PriorityQueue;

// 重要 优先队列

public class LC23 {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length == 0) return null;
        PriorityQueue<ListNode> q = new PriorityQueue<>((o1, o2) -> o1.val-o2.val);
        for (ListNode list : lists) {
            if(list!=null){
                q.add(list);
            }
        }
        ListNode dummyHead = new ListNode();
        ListNode temp = dummyHead;
        while (!q.isEmpty()){
            ListNode poll = q.poll();
            temp.next = poll;
            temp = poll;
            if(poll.next!=null){
                q.add(poll.next);
            }
        }
        return dummyHead.next;
    }
}
